#ifndef DOWNLOADMANAGER_H
#define DOWNLOADMANAGER_H

#include <QQueue>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QSslError>
#include <QUrl>
#include <stdio.h>

QT_BEGIN_NAMESPACE
class QSslError;
QT_END_NAMESPACE

QT_USE_NAMESPACE

class DownloadManager: public QObject
{
    Q_OBJECT

public:
    DownloadManager();

    //设置下载文件路径
    void set_path(const QString& path);

signals:

    //下载进度
    void download_progress(const QUrl &url, qint64 bytesReceived, qint64 bytesTotal);

    //下载完成
    void download_complete(const QUrl &url, bool ok, const QString file_name);

public slots:
    void add_download(const QUrl &url);

private slots:
    void download_finished(QNetworkReply *reply);
    void ssl_errors(const QList<QSslError> &errors);
    void replay_progress(qint64 bytesReceived, qint64 bytesTotal);

private:
    void download(void);
    bool save_to_disk(const QString &filename, QIODevice *data);
    QString save_name_get(const QUrl &url);

private:
    QNetworkAccessManager manager;
    QQueue<QUrl>          download_queue;
    QNetworkReply        *p_reply;
    QString               m_path;
};

#endif // DOWNLOADMANAGER_H
